﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Xml;
using System.Text;
using System.Data.Odbc;
using System.Windows.Forms;
 

namespace HealthExaminationSystem.AutoExportReport
{
    public static class SQLHelper
    {

        public static string SelectSQl(string strsql, string strcon)
        {
            string strres = "";
            using (SqlConnection con = new SqlConnection(strcon))
            {
                SqlCommand cmd = new SqlCommand(strsql, con);
                con.Open();
                strres = cmd.ExecuteScalar().ToString();
                WriteTxt(strsql);
                con.Dispose();
                con.Close();
            }
            return strres;

        }
        public static DataTable SelectData(string strsql, string strcon)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(strcon))
            {

                SqlCommand cmd = new SqlCommand(strsql, con);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                con.Dispose();
                con.Close();
                //WriteTxt(strsql);
            }
            return dt;

        }
        public static int UpdateSQl(string strsql, string strcon)
        {
            int ires = 0;
            using (SqlConnection con = new SqlConnection(strcon))
            {
                SqlCommand cmd = new SqlCommand(strsql, con);
                con.Open();
                ires = cmd.ExecuteNonQuery();
                WriteTxt(strsql);
                con.Dispose();
                con.Close();
            }
            return ires;

        }
   
        public static bool WriteTxt(string str)
        {
            try
            {
                FileStream fs = new FileStream(System.Environment.CurrentDirectory + "\\log\\Log.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public static bool AddSystemLog(string EmployeeId, string Object, string LogText, string strjcon)
        {
            LogText = LogText.Replace("'", "$");
            string strsql = "insert into systemlog(LogTime,EmployeeId,IP" +
                    ",Object,LogText) values(getdate(),'" + EmployeeId + "','192.168.0.1','" + Object + "','" + LogText + "')";
            UpdateSQl(strsql, strjcon);

            return true;
        }
        
    }
}